<?php

namespace App\Admin\Exports;

use App\Model\UserMsgLog;
use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class MsgLogExport extends ExcelExporter implements WithMapping, WithHeadings, ShouldAutoSize
{
    //这里是导出来的文件的名字和格式
    protected $fileName = '消息记录.xlsx';
    //这里是excel的标题
    public function headings(): array
    {
        return [
            '发送方ID',
            '发送方昵称',
            '接收方ID',
            '接受方昵称',
            '消息内容',
            '消息时间'
        ];
    }

    public function map($data): array
    {
        $msgDetail = UserMsgLog::unpackDetail($data->msg_detail, $data->msg_type);
        $content = '-';
        switch ($data->msg_type) {
            case UserMsgLog::RONG_TEXT_TYPE:
                $content = $msgDetail['content'];
                break;
            case UserMsgLog::RONG_SOUND_TYPE:
                $content = '【语音消息】' . "{$msgDetail['content']}";
                break;
            case UserMsgLog::RONG_IMG_TYPE:
                $content = '【图片消息】' . "{$msgDetail['content']}";
                break;
            case UserMsgLog::RONG_VIDEO_TYPE:
                $content = '【视频消息】';
                break;
        }
        return [
            $data->fromProfile->user_profile_id,
            $data->fromProfile->nick_name,
            $data->toProfile->user_profile_id,
            $data->toProfile->nick_name,
            $content,
            $data->created_at,
        ];
    }
}
